package com.awspaas.user.apps.cpq.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.tomcat.util.buf.UEncoder;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.BOCopyAPI;
import com.actionsoft.sdk.local.api.BOQueryAPI;

public class CPQ_GetPDFFilesWeb {

	public String getPDFFiles(UserContext userContext, String bindId) {
	try {
		/**
		 * 在项目报价清单台账和项目成本清单台账中生成对应的数据
		 * 
		 */
		//1.获取项目信息
		BOQueryAPI data=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_MSG").bindId(bindId);
		List<BO> bos=SDK.getBOAPI().query("BO_EU_CPQ_PROJECT_MSG_GRIDS").bindId(bindId).list();
		//2.创建项目成本清单信息
	ProcessInstance	processInstance=SDK.getProcessAPI().createBOProcessInstance("obj_478c5672750c4e0aadb7d5fbc7fdcb9a", userContext.getUID(), "");
	BOCopyAPI copyData=data.copyTo("BO_EU_CPQ_PROCJECT_SYSTEM_PRICE", processInstance.getId());	
	copyData.exec();	
	//3.创建项目报价清单信息
	ProcessInstance	processInstancePrice=SDK.getProcessAPI().createBOProcessInstance("obj_0fd66fb39a044f958a929d580de291d1", userContext.getUID(), "");
	BOCopyAPI copy=data.copyTo("BO_EU_CPQ_PROJECT_UNIT_PRICE", processInstancePrice.getId());
	copy.addNewData("DDSBJGHJ",data.detail().getString("XMZBJ") );
	copy.exec();
	//4.对项目成本清单子表和项目报价信息子表进行赋值
	List<BO> costs=new ArrayList<BO>();
	List<BO> prices=new ArrayList<BO>();
	String sql="select   XTMC,XTMH,SUM(CSJG) as TJG from  BO_EU_CPQ_PROJECT_MSG_GRIDS  where  BINDID=?  GROUP BY XTMH";
	
	if(bos.size()>0) {
		
		for (BO bo : bos) {
			BO cost=new BO();
			//创建项目成本清单模块成本明细
			if(!bo.getString("XTMC").equals("安装费")) {
				cost.set("LB", bo.getString("LB"));
				cost.set("XTMC", bo.getString("XTMC"));
				cost.set("MKMC",bo.getString("XDXH") );
				cost.set("XDXH", bo.getString("MKBH"));
				cost.set("SL",bo.getString("SL") );
				cost.set("DW", bo.getString("DW"));
				cost.set("DJ", bo.getString("DJ"));
				cost.set("ZJ", bo.getString("ZJ"));
				cost.set("XTMH", bo.getString("XTMH"));
				cost.set("GG",bo.getString("BZ") );
				costs.add(cost);
			}
			
			//创建项目报价清单模块价格明细
			BO priceData=new BO();
			priceData.set("XTMC", bo.getString("XTMC"));
			priceData.set("MKMC",bo.getString("XDXH") );
			priceData.set("MKBH", bo.getString("MKBH"));
			priceData.set("SL",bo.getString("SL") );
			priceData.set("DW", bo.getString("DW"));
			priceData.set("DJ", bo.getString("DJ"));
			priceData.set("XTMH", bo.getString("XTMH"));
			priceData.set("GG",bo.getString("BZ") );
			priceData.set("CSJG", bo.getString("CSJG"));
			prices.add(priceData);
			
			
		}
		//创建项目报价清单系统价格明细
		List<RowMap> maps=DBSql.getMaps(sql, new Object[] {bindId});
		List<BO> systems=new ArrayList<BO>();
		for (RowMap rowMap : maps) {
			String XTMC=rowMap.getString("XTMC");
			String XTMH=rowMap.getString("XTMH");
			String TJG=rowMap.getString("TJG");
			BO bo=new BO();
			bo.set("XTMC", XTMC);
			bo.set("XTMH", XTMH);
			bo.set("GG", "栋");
			bo.set("SL", "1");
			bo.set("JG", TJG);
			systems.add(bo);
		}
		//创建运费信息
		BO bo=new BO();
		bo.set("XTMC", "运费");
		bo.set("XTMH", "yf");
		bo.set("GG", "栋");
		bo.set("SL", "1");
		bo.set("JG",data.detail().getString("YSF"));
		systems.add(bo);
		//将项目成本明细和项目报价明细更新到表单中。
		SDK.getBOAPI().create("BO_EU_CPQ_PROCJECT_SYSTEM_PRICE_GRIDS", costs, processInstance.getId(), userContext.getUID());
		SDK.getBOAPI().create("BO_EU_PROJECT_UNIT_PRICE_MODEL", prices, processInstancePrice.getId(), userContext.getUID());
		SDK.getBOAPI().create("BO_EU_PROJECT_UNIT_PRICE_GRID", systems, processInstancePrice.getId(), userContext.getUID());

	}
	ResponseObject ro = ResponseObject.newOkResponse();
	Map<String, String> map=new HashMap<String, String>();
	map.put("costBindId", processInstance.getId());
	map.put("systemBindId", processInstancePrice.getId());
	ro.setData(map);
	return ro.toString();
	} catch (Exception e) {
		throw  e;
	}
		
	}

}
